package main

import (
	"flag"
	"fmt"

	"zero_server/rpc/admin/auth/authclient"
	"zero_server/rpc/admin/auth/gen/migrations"
	"zero_server/rpc/admin/auth/internal/config"
	adminadminserviceServer "zero_server/rpc/admin/auth/internal/server/adminadminservice"
	"zero_server/rpc/admin/auth/internal/svc"

	"github.com/zeromicro/go-zero/core/conf"
	"github.com/zeromicro/go-zero/core/service"
	"github.com/zeromicro/go-zero/zrpc"
	"google.golang.org/grpc"
	"google.golang.org/grpc/reflection"
)

var configFile = flag.String("f", "rpc/admin/auth/etc/auth.yaml", "the config file")

type Options struct {
	Migrate bool
}

func main() {

	var opt Options
	// 定义数据迁移
	flag.BoolVar(&opt.Migrate, "migrate", false, "migrate")
	flag.Parse()
	// 是否执行数据迁移文件
	if opt.Migrate {
		migrations.AutoMigrate()
	}

	var c config.Config
	conf.MustLoad(*configFile, &c)
	ctx := svc.NewServiceContext(c)

	s := zrpc.MustNewServer(c.RpcServerConf, func(grpcServer *grpc.Server) {
		authclient.RegisterAdminAdminServiceServer(grpcServer, adminadminserviceServer.NewAdminAdminServiceServer(ctx))

		if c.Mode == service.DevMode || c.Mode == service.TestMode {
			reflection.Register(grpcServer)
		}
	})
	defer s.Stop()

	fmt.Printf("Starting rpc server at %s...\n", c.ListenOn)
	s.Start()
}
